<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Http与Servlet | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/39.2cb0b4ee.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础语法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面向对象</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础高级</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>前端技术</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/84c5d2/" class="sidebar-link">HTML与CSS</a></li><li><a href="/pages/42b6b6/" class="sidebar-link">JavaScript与DOM</a></li><li><a href="/pages/1125b7/" class="sidebar-link">网络编程与Tomcat</a></li><li><a href="/pages/be0e03/" aria-current="page" class="active sidebar-link">Http与Servlet</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/be0e03/#一、今日学习内容与目标" class="sidebar-link">一、今日学习内容与目标</a></li><li class="sidebar-sub-header level2"><a href="/pages/be0e03/#二、http-协议-重点理解" class="sidebar-link">二、HTTP 协议(重点理解)</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/be0e03/#_2-1-http-协议概述" class="sidebar-link">2.1 HTTP 协议概述</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_2-1-2-http-协议概念和作用" class="sidebar-link">2.1.2 HTTP 协议概念和作用</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_2-1-2-http-协议的特点" class="sidebar-link">2.1.2 HTTP 协议的特点</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_2-1-3-http-协议的组成" class="sidebar-link">2.1.3 HTTP 协议的组成</a></li><li class="sidebar-sub-header level3"><a href="/pages/be0e03/#_2-2-请求概述-掌握" class="sidebar-link">2.2 请求概述（掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_2-2-1-浏览器查看请求内容" class="sidebar-link">2.2.1 浏览器查看请求内容</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_2-2-2-请求的组成内容" class="sidebar-link">2.2.2 请求的组成内容</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_2-2-3-get-和-post-请求的区别" class="sidebar-link">2.2.3 GET 和 POST 请求的区别</a></li><li class="sidebar-sub-header level3"><a href="/pages/be0e03/#_2-3-响应概述-掌握" class="sidebar-link">2.3 响应概述（掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_2-3-1-浏览器查看响应内容" class="sidebar-link">2.3.1 浏览器查看响应内容</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_2-3-2-响应的组成内容" class="sidebar-link">2.3.2 响应的组成内容</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_2-3-3-常见状态码及其含义" class="sidebar-link">2.3.3 常见状态码及其含义</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/be0e03/#三、servlet" class="sidebar-link">三、Servlet</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/be0e03/#_3-1-servlet-概述-理解" class="sidebar-link">3.1 Servlet 概述（理解）</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-1-1-servlet-是什么" class="sidebar-link">3.1.1 Servlet 是什么</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-1-2-servlet-的作用" class="sidebar-link">3.1.2 Servlet 的作用</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-1-3-servlet-的开发步骤" class="sidebar-link">3.1.3 Servlet 的开发步骤</a></li><li class="sidebar-sub-header level3"><a href="/pages/be0e03/#_3-2-servlet的第一个程序-掌握" class="sidebar-link">3.2 Servlet的第一个程序（掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-2-1-servlet-2-5-开发方式" class="sidebar-link">3.2.1 Servlet 2.5 开发方式</a></li><li class="sidebar-sub-header level5"><a href="/pages/be0e03/#_3-2-1-1-实现步骤" class="sidebar-link">3.2.1.1 实现步骤</a></li><li class="sidebar-sub-header level5"><a href="/pages/be0e03/#_3-2-1-2-实现演示" class="sidebar-link">3.2.1.2 实现演示</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-2-2-servlet-3-0-开发方法-注解方式配置" class="sidebar-link">3.2.2 Servlet 3.0 开发方法-注解方式配置</a></li><li class="sidebar-sub-header level5"><a href="/pages/be0e03/#_3-2-2-1-webservlet-注解" class="sidebar-link">3.2.2.1 @WebServlet 注解</a></li><li class="sidebar-sub-header level5"><a href="/pages/be0e03/#_3-2-2-2-注解开发-servlet-步骤" class="sidebar-link">3.2.2.2 注解开发 Servlet 步骤</a></li><li class="sidebar-sub-header level5"><a href="/pages/be0e03/#_3-2-2-3-注解开发servlet演示" class="sidebar-link">3.2.2.3 注解开发Servlet演示</a></li><li class="sidebar-sub-header level3"><a href="/pages/be0e03/#_3-3-servlet-的生命周期-掌握" class="sidebar-link">3.3 Servlet 的生命周期（掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-3-1-思考三个问题" class="sidebar-link">3.3.1 思考三个问题</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-3-2-servlet的生命周期" class="sidebar-link">3.3.2 Servlet的生命周期</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-3-3-servlet-接口中生命周期方法" class="sidebar-link">3.3.3 Servlet  接口中生命周期方法</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-3-4-总结回顾" class="sidebar-link">3.3.4 总结回顾</a></li><li class="sidebar-sub-header level3"><a href="/pages/be0e03/#_3-4-servlet-的请求流程-掌握" class="sidebar-link">3.4 Servlet 的请求流程（掌握）</a></li><li class="sidebar-sub-header level3"><a href="/pages/be0e03/#_3-5-servlet-的继承体系-了解" class="sidebar-link">3.5 Servlet 的继承体系（了解）</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-5-1-servlet的继承结构" class="sidebar-link">3.5.1 Servlet的继承结构</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-5-2-genericservlet类" class="sidebar-link">3.5.2 GenericServlet类</a></li><li class="sidebar-sub-header level5"><a href="/pages/be0e03/#_3-5-2-1-genericservlet类概述" class="sidebar-link">3.5.2.1 GenericServlet类概述</a></li><li class="sidebar-sub-header level5"><a href="/pages/be0e03/#_3-5-2-2-继承-genericservlet-开发-servlet" class="sidebar-link">3.5.2.2 继承 GenericServlet 开发 Servlet</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-5-3-httpservlet类" class="sidebar-link">3.5.3 HttpServlet类</a></li><li class="sidebar-sub-header level5"><a href="/pages/be0e03/#_3-5-3-1-httpservlet类概述" class="sidebar-link">3.5.3.1 HttpServlet类概述</a></li><li class="sidebar-sub-header level5"><a href="/pages/be0e03/#_3-5-3-2-继承-httpservlet-开发-servlet" class="sidebar-link">3.5.3.2 继承 HttpServlet 开发 Servlet</a></li><li class="sidebar-sub-header level5"><a href="/pages/be0e03/#_3-5-3-3-service-方法源码分析" class="sidebar-link">3.5.3.3 service 方法源码分析</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-5-4-小结-掌握" class="sidebar-link">3.5.4 小结（掌握）</a></li><li class="sidebar-sub-header level3"><a href="/pages/be0e03/#_3-6-servletconfig-对象-掌握" class="sidebar-link">3.6 ServletConfig 对象（掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-6-1-作用" class="sidebar-link">3.6.1 作用</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-6-2-常用方法" class="sidebar-link">3.6.2 常用方法</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-6-3-使用演示" class="sidebar-link">3.6.3 使用演示</a></li><li class="sidebar-sub-header level3"><a href="/pages/be0e03/#_3-7-loadonstartup-属性-了解" class="sidebar-link">3.7 loadOnStartup 属性（了解）</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-7-1-思考问题" class="sidebar-link">3.7.1 思考问题</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-7-2-作用" class="sidebar-link">3.7.2 作用</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-7-3-用法" class="sidebar-link">3.7.3 用法</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-7-4-使用场景" class="sidebar-link">3.7.4 使用场景</a></li><li class="sidebar-sub-header level3"><a href="/pages/be0e03/#_3-8-servlet映射细节-了解" class="sidebar-link">3.8 Servlet映射细节（了解）</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-8-1-配置多个路径" class="sidebar-link">3.8.1 配置多个路径</a></li><li class="sidebar-sub-header level5"><a href="/pages/be0e03/#_3-8-1-1-配置方式1" class="sidebar-link">3.8.1.1 配置方式1</a></li><li class="sidebar-sub-header level5"><a href="/pages/be0e03/#_3-8-1-2-配置方式2" class="sidebar-link">3.8.1.2 配置方式2</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-8-2-通配符映射" class="sidebar-link">3.8.2 通配符映射 *</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-8-3-映射注意事项" class="sidebar-link">3.8.3 映射注意事项</a></li><li class="sidebar-sub-header level4"><a href="/pages/be0e03/#_3-8-4-面试题" class="sidebar-link">3.8.4 面试题</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/be0e03/#四、小结与作业" class="sidebar-link">四、小结与作业</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/be0e03/#_4-1-小结" class="sidebar-link">4.1 小结</a></li><li class="sidebar-sub-header level3"><a href="/pages/be0e03/#_4-2-作业" class="sidebar-link">4.2 作业</a></li></ul></li></ul></li><li><a href="/pages/9902e4/" class="sidebar-link">请求与响应</a></li><li><a href="/pages/18345f/" class="sidebar-link">JSP与EL与JSTL</a></li><li><a href="/pages/aa67fd/" class="sidebar-link">WEB版CRUD</a></li><li><a href="/pages/baec2f/" class="sidebar-link">Cookie与Session</a></li><li><a href="/pages/73e2cb/" class="sidebar-link">文件上传与下载和登录</a></li><li><a href="/pages/f99e33/" class="sidebar-link">项目实战-MIS系统</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>NoSQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=%E5%89%8D%E7%AB%AF%E6%8A%80%E6%9C%AF" title="分类" data-v-06225672>前端技术</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-03-03</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">Http与Servlet<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="http与servlet"><a href="#http与servlet" class="header-anchor">#</a> Http与Servlet</h1> <h2 id="一、今日学习内容与目标"><a href="#一、今日学习内容与目标" class="header-anchor">#</a> 一、今日学习内容与目标</h2> <ul><li>理解什么是 HTTP 协议以及作用</li> <li>掌握请求头和响应头信息的结构</li> <li>掌握如何通过浏览器去查看请求头和响应头信息</li> <li>掌握 GET 请求和 POST 请求的区别 <code>***</code></li> <li>能够使用 IDEA 编写 Servlet（xml 和注解方式）</li> <li>能够说出 Servlet 生命周期</li> <li>理解并掌握 Servlet 的请求流程</li> <li>掌握 Servlet 继承体系的小结内容</li> <li>掌握 ServletConfig 对象的作用</li> <li>了解 Servlet 映射细节 <code>***</code></li></ul> <h2 id="二、http-协议-重点理解"><a href="#二、http-协议-重点理解" class="header-anchor">#</a> 二、HTTP 协议(重点理解)</h2> <h3 id="_2-1-http-协议概述"><a href="#_2-1-http-协议概述" class="header-anchor">#</a> 2.1 HTTP 协议概述</h3> <h4 id="_2-1-2-http-协议概念和作用"><a href="#_2-1-2-http-协议概念和作用" class="header-anchor">#</a> 2.1.2 HTTP 协议概念和作用</h4> <ul><li>概念：HTTP 是 <strong>H</strong>yper<strong>T</strong>ext <strong>T</strong>ransfer <strong>P</strong>rotocol (超文本传输协议)的简写，它是 TCP/IP 协议之上的一个应用层协议。简单理解就是 HTTP 协议底层是对 TCP/IP 协议的封装。</li> <li><strong>作用</strong>：用于规定浏览器和服务器之间数据传输的格式</li> <li>须知：HTTP 协议默认端口号是 80，Tomcat 默认是 8080</li></ul> <h4 id="_2-1-2-http-协议的特点"><a href="#_2-1-2-http-协议的特点" class="header-anchor">#</a> 2.1.2 HTTP 协议的特点</h4> <ol><li><p>无状态：协议对客户端没有状态存储，对事务处理没有“记忆”能力。</p></li> <li><p>HTTP/1.0，每次请求需要通过 TCP 三次握手建立连接，请求结束通过四次挥手断开连接，一次连接只能处理一个请求数据的传输。HTTP/1.1 一次连接成功之后可以处理·。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/1589124260730.png" alt="1589124260730"></p></li> <li><p>基于请求和响应：基本的特性，由客户端发起请求，服务端响应</p></li></ol> <h4 id="_2-1-3-http-协议的组成"><a href="#_2-1-3-http-协议的组成" class="header-anchor">#</a> 2.1.3 HTTP 协议的组成</h4> <ul><li>请求：浏览器访问服务器（带数据给服务器）的过程</li> <li>响应：服务器反馈数据给浏览器的过程</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/1589123602555.png" alt="1589123602555"></p> <h3 id="_2-2-请求概述-掌握"><a href="#_2-2-请求概述-掌握" class="header-anchor">#</a> 2.2 请求概述（掌握）</h3> <h4 id="_2-2-1-浏览器查看请求内容"><a href="#_2-2-1-浏览器查看请求内容" class="header-anchor">#</a> 2.2.1 浏览器查看请求内容</h4> <ol><li>在 HTML 页面上制作 2 个表单提交页面，用户名和密码，get 提交和 post 提交按钮，查看 HTTP 请求</li></ol> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>en<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>UTF-8<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>Title<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!--login是一个servlet--&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span>GET提交<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span> <span class="token attr-name">action</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>login<span class="token punctuation">&quot;</span></span> <span class="token attr-name">method</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>get<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        用户名：
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>text<span class="token punctuation">&quot;</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>username<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">/&gt;</span></span>
        密码：
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>password<span class="token punctuation">&quot;</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>pwd<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">/&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>submit<span class="token punctuation">&quot;</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>登录<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>hr</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span>POST提交<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span> <span class="token attr-name">action</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>login<span class="token punctuation">&quot;</span></span> <span class="token attr-name">method</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>post<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        用户名：
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>text<span class="token punctuation">&quot;</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>username<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">/&gt;</span></span>
        密码：
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>password<span class="token punctuation">&quot;</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>pwd<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">/&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>submit<span class="token punctuation">&quot;</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>登录<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><ol start="2"><li>查看浏览器与服务器的通讯</li></ol> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200422182159359.png" alt="image-20200422182159359"></p> <h4 id="_2-2-2-请求的组成内容"><a href="#_2-2-2-请求的组成内容" class="header-anchor">#</a> 2.2.2 请求的组成内容</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200422182424978.png" alt="image-20200422182424978"></p> <ol><li>请求行：请求方式  请求地址  协议和版本</li> <li>请求头：由各种键值对组成，是浏览器发送给服务器的信息，服务器可以获得这些信息</li> <li>请求体(只有POST请求才有请求体，数据在请求体中发送，GET方法没有请求体)</li></ol> <h4 id="_2-2-3-get-和-post-请求的区别"><a href="#_2-2-3-get-和-post-请求的区别" class="header-anchor">#</a> 2.2.3 GET 和 POST 请求的区别</h4> <table><thead><tr><th></th> <th>POST 方式</th> <th>GET 方式</th></tr></thead> <tbody><tr><td>地址栏</td> <td>数据在请求体中发送<br>地址上不显示参数</td> <td>数据请求行中发送<br>在地址栏上可以看参数</td></tr> <tr><td>安全性</td> <td>安全性更高</td> <td>安全性更低</td></tr> <tr><td>大小</td> <td>数据没有限制大小</td> <td>请求的数据不能超过2K</td></tr> <tr><td>缓存</td> <td>发送数据不使用本地缓存</td> <td>从服务器上获取数据，会使用缓存。<br>如果本地浏览器已经缓存页面，而且服务器的数据<br>没有修改，不再从服务上得到数据。</td></tr> <tr><td>类型</td> <td>表单 method 为 post</td> <td>表单 method 为 get，超链接，浏览器直接访问资源等</td></tr></tbody></table> <p><strong>哪些请求是 GET ，哪些请求是POST</strong></p> <p>简单理解： 除了 form 表单中 method=“post” 为 post 请求，其他都是 get 请求.</p> <h3 id="_2-3-响应概述-掌握"><a href="#_2-3-响应概述-掌握" class="header-anchor">#</a> 2.3 响应概述（掌握）</h3> <h4 id="_2-3-1-浏览器查看响应内容"><a href="#_2-3-1-浏览器查看响应内容" class="header-anchor">#</a> 2.3.1 浏览器查看响应内容</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200422182942187.png" alt="image-20200422182942187"></p> <h4 id="_2-3-2-响应的组成内容"><a href="#_2-3-2-响应的组成内容" class="header-anchor">#</a> 2.3.2 响应的组成内容</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200422183339694.png" alt="image-20200422183339694"></p> <ol><li>响应行：协议和版本 状态码  状态信息</li> <li>响应头：由各种键值对组成，是服务器返回给浏览器的信息。</li> <li>响应体：服务器发送给浏览器的数据，如果是网页是字符类型的数据，如果是图片是二进制的数据</li></ol> <h4 id="_2-3-3-常见状态码及其含义"><a href="#_2-3-3-常见状态码及其含义" class="header-anchor">#</a> 2.3.3 常见状态码及其含义</h4> <table><thead><tr><th><strong>状态码</strong></th> <th><strong>含义</strong></th></tr></thead> <tbody><tr><td><strong>200</strong></td> <td>服务器正确响应</td></tr> <tr><td>302</td> <td>浏览器端进行页面跳转</td></tr> <tr><td>304</td> <td>服务器告诉浏览器使用本地缓存信息</td></tr> <tr><td><strong>404</strong></td> <td>请求的资源找不到</td></tr> <tr><td>405</td> <td>Servlet 执行了原生的 doGet 或 doPost 方法</td></tr> <tr><td><strong>500</strong></td> <td>服务器内部错误，通常是 Java 代码有问题</td></tr></tbody></table> <h2 id="三、servlet"><a href="#三、servlet" class="header-anchor">#</a> 三、Servlet</h2> <h3 id="_3-1-servlet-概述-理解"><a href="#_3-1-servlet-概述-理解" class="header-anchor">#</a> 3.1 Servlet 概述（理解）</h3> <h4 id="_3-1-1-servlet-是什么"><a href="#_3-1-1-servlet-是什么" class="header-anchor">#</a> 3.1.1 Servlet 是什么</h4> <p>Servlet（Server Applet）是Java Servlet的简称，称为小服务程序或服务连接器，用Java编写的服务器端程序，具有独立于平台和协议的特性，主要功能在于交互式地浏览和生成数据，生成动态Web内容。</p> <p>狭义的Servlet是指Java语言实现的一个接口，广义的Servlet是指任何实现了这个Servlet接口的类，一般情况下，人们将Servlet理解为后者。Servlet运行于支持Java的应用服务器中。从原理上讲，Servlet可以响应任何类型的请求，但绝大多数情况下Servlet只用来扩展基于HTTP协议的Web服务器。</p> <p>最早支持Servlet标准的是JavaSoft的Java Web Server，此后，一些其它的基于Java的Web服务器开始支持标准的Servlet。</p> <p>总结：<strong>Servlet 是运行在 Web服务器(如Tomcat服务器)的，使用 Java编写的小应用程序。</strong></p> <h4 id="_3-1-2-servlet-的作用"><a href="#_3-1-2-servlet-的作用" class="header-anchor">#</a> 3.1.2 Servlet 的作用</h4> <ol><li>开发动态资源：Servlet 也是服务器的一种<strong>资源</strong>，可以供外界(浏览器)去访问。</li> <li>接收浏览器请求并响应数据给浏览器.</li></ol> <h4 id="_3-1-3-servlet-的开发步骤"><a href="#_3-1-3-servlet-的开发步骤" class="header-anchor">#</a> 3.1.3 Servlet 的开发步骤</h4> <ol><li><p>创建一个类实现 javax.servlet.Servlet 接口</p></li> <li><p>实现接口中的所有方法</p></li> <li><p>在 service 方法处理请求和响应数据</p></li> <li><p>配置 Servlet 的访问地址(供外界:浏览器访问)</p> <ul><li>可以通过 web.xml 文件配置</li> <li>可以通过注解配置</li></ul></li></ol> <h3 id="_3-2-servlet的第一个程序-掌握"><a href="#_3-2-servlet的第一个程序-掌握" class="header-anchor">#</a> 3.2 Servlet的第一个程序（掌握）</h3> <h4 id="_3-2-1-servlet-2-5-开发方式"><a href="#_3-2-1-servlet-2-5-开发方式" class="header-anchor">#</a> 3.2.1 Servlet 2.5 开发方式</h4> <p>​				使用 web.xml 文件配置</p> <h5 id="_3-2-1-1-实现步骤"><a href="#_3-2-1-1-实现步骤" class="header-anchor">#</a> 3.2.1.1 实现步骤</h5> <ol><li><p>创建一个 HelloServlet 实现 Servlet 接口</p></li> <li><p>重写接口中的所有方法，在 service 方法中输出内容到控制台</p></li> <li><p>配置 web/WEB-INF/web.xml 文件</p></li></ol> <h5 id="_3-2-1-2-实现演示"><a href="#_3-2-1-2-实现演示" class="header-anchor">#</a> 3.2.1.2 实现演示</h5> <ol><li><p>创建web工程</p> <p>File -&gt; new Project-&gt; Java Enterprise -&gt; Web Application -&gt; Finish</p></li> <li><p>创建好的Web项目结构：idea 生成或者 tomcat 案例中拷贝 web.xml 文件</p></li></ol> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200606222853447.png" alt="image-20200606222853447"></p> <p>使用工具生成 web.xml 的方式</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200511112911876.png" alt="image-20200511112911876"></p> <ol start="3"><li>在 src 创建包，在包下创建 HelloServlet 实现 Servlet 接口重写所有方法，在 service 方法中往控制台输出：Hello Servlet!</li></ol> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span></span>_01_servlet<span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token operator">*</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>
<span class="token comment">/**
 * 创建一个类实现Servlet接口
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HelloServlet</span> <span class="token keyword">implements</span> <span class="token class-name">Servlet</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token class-name">ServletConfig</span> servletConfig<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">ServletConfig</span> <span class="token function">getServletConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 在该方法中往控制台输出：Hello Servlet!</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">ServletRequest</span> servletRequest<span class="token punctuation">,</span> <span class="token class-name">ServletResponse</span> servletResponse<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Hello Servlet!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getServletInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">destroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><ol start="4"><li>编辑 web.xml 中配置 servlet，设置访问地址为 /hello</li></ol> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token prolog">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>web-app</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://java.sun.com/xml/ns/javaee<span class="token punctuation">&quot;</span></span>
           <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">&quot;</span></span>
           <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd<span class="token punctuation">&quot;</span></span>
           <span class="token attr-name">version</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>2.5<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!-- servlet 信息配置 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet</span><span class="token punctuation">&gt;</span></span>
        <span class="token comment">&lt;!--servlet 名字--&gt;</span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-name</span><span class="token punctuation">&gt;</span></span>HelloServlet<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-name</span><span class="token punctuation">&gt;</span></span>
        <span class="token comment">&lt;!-- servlet类全限定名--&gt;</span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-class</span><span class="token punctuation">&gt;</span></span>cn.wolfcode._01_servlet.HelloServlet<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-class</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!--servlet 访问地址配置 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-mapping</span><span class="token punctuation">&gt;</span></span>
        <span class="token comment">&lt;!--servlet 名字：与上面的名字相同 --&gt;</span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-name</span><span class="token punctuation">&gt;</span></span>HelloServlet<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-name</span><span class="token punctuation">&gt;</span></span>
        <span class="token comment">&lt;!--  浏览器访问地址，必须以/开头 --&gt;</span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>url-pattern</span><span class="token punctuation">&gt;</span></span>/hello<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>url-pattern</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-mapping</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>web-app</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><ol start="5"><li>部署项目并通过浏览器访问 Servlet，如果无项目名，端口为80，则为 localhost/hello</li></ol> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200511074636275.png" alt="image-20200511074636275"></p> <ul><li>控制台结果：</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240221192355507.png" alt="image-20240221192355507"></p> <p><strong>热部署的配置</strong>: 可以不用重启服务器直接访问改过的页面.</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200511113953483.png" alt="image-20200511113953483"></p> <h4 id="_3-2-2-servlet-3-0-开发方法-注解方式配置"><a href="#_3-2-2-servlet-3-0-开发方法-注解方式配置" class="header-anchor">#</a> 3.2.2 Servlet 3.0 开发方法-注解方式配置</h4> <h5 id="_3-2-2-1-webservlet-注解"><a href="#_3-2-2-1-webservlet-注解" class="header-anchor">#</a> 3.2.2.1 @WebServlet 注解</h5> <p>Servlet3.0 之后新增了一些注解，简化的 JavaWeb 代码开发，可以省略 web.xml配置文件</p> <table><thead><tr><th>@WebServlet注解属性</th> <th>说明</th></tr></thead> <tbody><tr><td>name =  &quot;HelloServlet&quot;</td> <td>Servlet名字，等价<code>&lt;servlet-name&gt;HelloServlet&lt;/servlet-name&gt;</code></td></tr> <tr><td>urlPatterns =  &quot;/hello&quot;</td> <td>访问地址，等价 <code>&lt;url-pattern&gt;/hello&lt;/url-pattern&gt;</code></td></tr> <tr><td>value=&quot;/hello&quot;</td> <td>如果其它属性都不写，可以省略 value，只写访问地址即可。<br>不能与urlPatterns同时指定</td></tr></tbody></table> <h5 id="_3-2-2-2-注解开发-servlet-步骤"><a href="#_3-2-2-2-注解开发-servlet-步骤" class="header-anchor">#</a> 3.2.2.2 注解开发 Servlet 步骤</h5> <ol><li>使用注解，必须是 Servlet3.0 之后才可以，Tomcat8 已经实现了3.0规范</li> <li>使用注解，必须是 JavaEE 6.0 版本以上</li> <li>创建类实现 Servlet 接口，在类上使用 @WebServlet 注解中添加 urlPatterns=&quot;/hello&quot;或 value=&quot;/hello&quot;，作为请求路径</li></ol> <h5 id="_3-2-2-3-注解开发servlet演示"><a href="#_3-2-2-3-注解开发servlet演示" class="header-anchor">#</a> 3.2.2.3 注解开发Servlet演示</h5> <ol><li><p>创建 JavaEE6 模块(版本必须大于 6 才能使用注解)</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200511074332057.png" alt="image-20200511074332057"></p></li> <li><p>创建好的 Web 目录结构</p></li></ol> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240221192459732.png" alt="image-20240221192459732"></p> <ol start="3"><li>在 src 创建包，在包下创建 HttpServlet 实现 Servlet 接口重写所有方法，在 service 方法中往控制台输出：Hello Servlet!    <strong>使用注解 @WebServlet 配置访问地址</strong></li></ol> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span></span>_01servlet<span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token operator">*</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">WebServlet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@WebServlet</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">&quot;HelloServlet&quot;</span><span class="token punctuation">,</span>urlPatterns <span class="token operator">=</span> <span class="token string">&quot;/hello&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HelloServlet</span> <span class="token keyword">implements</span> <span class="token class-name">Servlet</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token class-name">ServletConfig</span> servletConfig<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">ServletConfig</span> <span class="token function">getServletConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 在该方法中往控制台输出：Hello Servlet!</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">ServletRequest</span> servletRequest<span class="token punctuation">,</span> <span class="token class-name">ServletResponse</span> servletResponse<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Hello Servlet!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getServletInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">destroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><ol start="4"><li><p>部署项目，浏览器地址栏输入：http://localhost:8080/second/hello ，如果无项目名，端口为80，则为</p> <p>http://localhost/hello</p></li></ol> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200511074752560.png" alt="image-20200511074752560"></p> <ul><li>控制台执行结果：</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Hello</span> <span class="token class-name">Servlet</span><span class="token operator">!</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_3-3-servlet-的生命周期-掌握"><a href="#_3-3-servlet-的生命周期-掌握" class="header-anchor">#</a> 3.3 Servlet 的生命周期（掌握）</h3> <p>生命周期： 从出生到死亡以及中间所经历的过程则称为一个生命周期</p> <p>类的生命周期： 创建对象--&gt; 运行操作--&gt;销毁操作</p> <h4 id="_3-3-1-思考三个问题"><a href="#_3-3-1-思考三个问题" class="header-anchor">#</a> 3.3.1 思考三个问题</h4> <ul><li>[x] Servlet 对象是谁负责创建的？</li> <li>[x] Servlet 对象是什么时候创建的？</li> <li>[x] Servlet 对象是什么时候销毁的？</li></ul> <h4 id="_3-3-2-servlet的生命周期"><a href="#_3-3-2-servlet的生命周期" class="header-anchor">#</a> 3.3.2 Servlet的生命周期</h4> <p>Servlet 对象的生命周期：Servlet 创建对象--&gt;初始化操作--&gt; 运行操作--&gt;销毁操作
Web 服务器管理了 Servlet 的生命周期，Servlet 对象整个过程都是 Web 服务器来管理的。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240221192539999.png" alt="image-20240221192539999"></p> <h4 id="_3-3-3-servlet-接口中生命周期方法"><a href="#_3-3-3-servlet-接口中生命周期方法" class="header-anchor">#</a> 3.3.3 Servlet  接口中生命周期方法</h4> <table><thead><tr><th>生命周期方法</th> <th>作用</th> <th>运行次数</th></tr></thead> <tbody><tr><td>构造方法</td> <td>在对象实例化的时候执行<br>必须有公共的无参数构造方法</td> <td>1次</td></tr> <tr><td>void init(ServletConfig  config)</td> <td>在初始化的时候执行</td> <td>1次</td></tr> <tr><td>void  service(ServletRequest req, ServletResponse res)</td> <td>每次请求都会执行</td> <td>n次</td></tr> <tr><td>void destroy()</td> <td>在服务器正常关闭的时候</td> <td>1次</td></tr></tbody></table> <ul><li>生命周期方法执行顺序图</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240221192606425.png" alt="image-20240221192606425"></p> <h4 id="_3-3-4-总结回顾"><a href="#_3-3-4-总结回顾" class="header-anchor">#</a> 3.3.4 总结回顾</h4> <ul><li>Servlet 对象是谁负责创建的？答：Web服务器(Tomcat)负责创建</li> <li>Servlet 对象是什么时候创建的？答：用户第1次访问的时候</li> <li>Servlet 对象是什么时候销毁的？答：服务器正常关闭或重启时销毁</li> <li>Servlet 是单例还是多例的？答：Servlet 只在第一次访问时创建了对象，所以是单例的</li></ul> <h3 id="_3-4-servlet-的请求流程-掌握"><a href="#_3-4-servlet-的请求流程-掌握" class="header-anchor">#</a> 3.4 Servlet 的请求流程（掌握）</h3> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/img19.png" alt=""></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>localhost<span class="token operator">:</span><span class="token number">8080</span><span class="token operator">/</span>day11<span class="token operator">/</span>hello
<span class="token number">1.</span> 浏览器发送请求，<span class="token class-name">Tomcat</span>接收到请求并通过解析请求地址获取到要访问的项目和资源路径
  	项目访问路径：<span class="token operator">/</span>day11
  	资源路径：   <span class="token operator">/</span>hello
<span class="token number">2.</span> <span class="token class-name">Tomcat</span>服务器启动时内部会扫描 day11项目下的所有 <span class="token class-name">Servlet</span>：获得每一个 <span class="token class-name">Servlet</span> 的访问地址并存储到集合中：   
    <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> map1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	map1<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;/hello&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;cn.wolfcode.servlet.HelloServlet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token number">3.</span> 将资源路径 <span class="token operator">/</span>hello 作为键从 map1 集合中获得值：得到类全限定名
<span class="token number">4.</span> 需要判断是否是第一次访问<span class="token operator">:</span>
	<span class="token class-name">Servlet</span>实例缓存池<span class="token operator">:</span> <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Servlet</span><span class="token punctuation">&gt;</span></span> map2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>map2<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>“全限定名”<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
		<span class="token comment">// 第一次访问,执行第5步</span>
	<span class="token punctuation">}</span> <span class="token keyword">else</span><span class="token punctuation">{</span>
		<span class="token comment">// 第N次,直接执行第7步</span>
	<span class="token punctuation">}</span>
<span class="token number">5.</span> 通过反射实例化这个 <span class="token class-name">Servlet</span> 对象<span class="token punctuation">,</span>存入实例缓存池<span class="token punctuation">(</span>map2<span class="token punctuation">)</span> 中<span class="token punctuation">.</span>
<span class="token number">6.</span> <span class="token class-name">Tomcat</span> 创建 <span class="token class-name">ServletConfig</span> 对象然后调用 init 方法
<span class="token number">7.</span> 创建 request 和 response 对象
<span class="token number">8.</span> 调用 service 方法，将 request和 response 对象传递进来<span class="token punctuation">,</span>在 service 方法中通过 response对象返回输出到浏览器，在浏览器上显示出来。
<span class="token number">9.</span> 等待下一次的访问
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="_3-5-servlet-的继承体系-了解"><a href="#_3-5-servlet-的继承体系-了解" class="header-anchor">#</a> 3.5 Servlet 的继承体系（了解）</h3> <h4 id="_3-5-1-servlet的继承结构"><a href="#_3-5-1-servlet的继承结构" class="header-anchor">#</a> 3.5.1 Servlet的继承结构</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240221192642525.png" alt="image-20240221192642525"></p> <h4 id="_3-5-2-genericservlet类"><a href="#_3-5-2-genericservlet类" class="header-anchor">#</a> 3.5.2 GenericServlet类</h4> <h5 id="_3-5-2-1-genericservlet类概述"><a href="#_3-5-2-1-genericservlet类概述" class="header-anchor">#</a> 3.5.2.1 GenericServlet类概述</h5> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240221192710005.png" alt="image-20240221192710005"></p> <p>​		默认实现了 Servlet 和 ServletConfig 这两个接口，它的子类是 HttpServlet，如果我们写的Servlet使用的是 Http 协议，建议继承于 HttpServlet。现在所有的浏览器都是使用http协议，所以我们以后都是继承于 HttpServlet 类就可以了。</p> <h5 id="_3-5-2-2-继承-genericservlet-开发-servlet"><a href="#_3-5-2-2-继承-genericservlet-开发-servlet" class="header-anchor">#</a> 3.5.2.2 继承 GenericServlet 开发 Servlet</h5> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span></span>_02genericServlet<span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token class-name">GenericServlet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token class-name">ServletException</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token class-name">ServletRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token class-name">ServletResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">WebServlet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@WebServlet</span><span class="token punctuation">(</span>urlPatterns <span class="token operator">=</span> <span class="token string">&quot;/demo&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DemoServlet</span> <span class="token keyword">extends</span> <span class="token class-name">GenericServlet</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">ServletRequest</span> servletRequest<span class="token punctuation">,</span> <span class="token class-name">ServletResponse</span> servletResponse<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;我是GenericServlet子类&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><ul><li>浏览器访问运行效果</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240221192729892.png" alt="image-20240221192729892"></p> <h4 id="_3-5-3-httpservlet类"><a href="#_3-5-3-httpservlet类" class="header-anchor">#</a> 3.5.3 HttpServlet类</h4> <h5 id="_3-5-3-1-httpservlet类概述"><a href="#_3-5-3-1-httpservlet类概述" class="header-anchor">#</a> 3.5.3.1 HttpServlet类概述</h5> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240221192803344.png" alt="image-20240221192803344"></p> <h5 id="_3-5-3-2-继承-httpservlet-开发-servlet"><a href="#_3-5-3-2-继承-httpservlet-开发-servlet" class="header-anchor">#</a> 3.5.3.2 继承 HttpServlet 开发 Servlet</h5> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span></span>_03httpservlet<span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token class-name">ServletException</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">WebServlet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServlet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServletRequest</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpServletResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@WebServlet</span><span class="token punctuation">(</span>urlPatterns <span class="token operator">=</span> <span class="token string">&quot;/demo02&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DemoServlet</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;我是HttpServlet的子类&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><ul><li>浏览器访问运行效果</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200423132532180.png" alt="image-20200423132532180"></p> <h5 id="_3-5-3-3-service-方法源码分析"><a href="#_3-5-3-3-service-方法源码分析" class="header-anchor">#</a> 3.5.3.3 service 方法源码分析</h5> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">HttpServlet</span> <span class="token keyword">extends</span> <span class="token class-name">GenericServlet</span> <span class="token punctuation">{</span>
    <span class="token comment">//定义了一些常量</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">String</span> <span class="token constant">METHOD_GET</span> <span class="token operator">=</span> <span class="token string">&quot;GET&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">String</span> <span class="token constant">METHOD_POST</span> <span class="token operator">=</span> <span class="token string">&quot;POST&quot;</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">HttpServlet</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">}</span>
  
	 <span class="token comment">// 自己的service方法</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
	      <span class="token comment">// 得到请求的方法：返回GET或POST</span>
        <span class="token class-name">String</span> method <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		    <span class="token comment">// 如果是GET请求，调用doGet</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>method<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">&quot;GET&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
              <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">doGet</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> resp<span class="token punctuation">)</span><span class="token punctuation">;</span>
	    			 <span class="token comment">// 如果是POST请求，调用POST方法</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>method<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">&quot;POST&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">doPost</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> resp<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
		    		<span class="token comment">// 如果所有的都不匹配，发送一个错误</span>
            resp<span class="token punctuation">.</span><span class="token function">sendError</span><span class="token punctuation">(</span><span class="token number">501</span><span class="token punctuation">,</span> errMsg<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 发送一个错误信息</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

	  <span class="token comment">// Servlet接口中定义的方法</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">ServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">ServletResponse</span> res<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">;</span>   <span class="token comment">// 请求子接口</span>
        <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">;</span>   <span class="token comment">// 响应子接口</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            request <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span><span class="token punctuation">)</span>req<span class="token punctuation">;</span>
            response <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">HttpServletResponse</span><span class="token punctuation">)</span>res<span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">ClassCastException</span> var6<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">ServletException</span><span class="token punctuation">(</span><span class="token string">&quot;non-HTTP request or response&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
				<span class="token comment">// 调用重置的service方法</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">service</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> response<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><h4 id="_3-5-4-小结-掌握"><a href="#_3-5-4-小结-掌握" class="header-anchor">#</a> 3.5.4 小结（掌握）</h4> <ol><li><p>GenericServlet 是一个通用的 Servlet，可以用来处理各种协议发出的请求和响应。</p></li> <li><p>HttpServlet 是专门用来处理 HTTP 协议发送的请求，现在所有的浏览器发请求都是使用 HTTP 协议，因此以后我们开发 Servlet 只需要继承 HttpServlet 即可，可以按照如下步骤开发：</p></li></ol> <div class="language- line-numbers-mode"><pre class="language-text"><code>1. 创建类继承 HttpServlet
2. 重写参数带着 Http 开头的 service 方法：在该方法中处理请求并响应数据。
	 注意事项：不要在该方法中调用父类的 service 方法(405)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_3-6-servletconfig-对象-掌握"><a href="#_3-6-servletconfig-对象-掌握" class="header-anchor">#</a> 3.6 ServletConfig 对象（掌握）</h3> <h4 id="_3-6-1-作用"><a href="#_3-6-1-作用" class="header-anchor">#</a> 3.6.1 作用</h4> <ul><li>用来封装 Servlet 初始化的时候的一些配置信息</li> <li>为什么要配置初始化参数？
<ul><li>解决硬编码的缺点：因为是写在源代码中，一旦写死，很难修改。维护不太方便。建议把一些与Servlet有关的配置信息，写在web.xml中，后期可以方便修改和维护</li></ul></li></ul> <h4 id="_3-6-2-常用方法"><a href="#_3-6-2-常用方法" class="header-anchor">#</a> 3.6.2 常用方法</h4> <table><thead><tr><th>ServletConfig接口常用方法</th> <th>说明</th></tr></thead> <tbody><tr><td>String getInitParameter(&quot;参数名&quot;)</td> <td>通过指定的参数名得到参数</td></tr></tbody></table> <h4 id="_3-6-3-使用演示"><a href="#_3-6-3-使用演示" class="header-anchor">#</a> 3.6.3 使用演示</h4> <ul><li><p>需求：根据不同的编码格式响应数据给浏览器，编码格式作为Servlet的初始化配置参数，在Servlet的service方法中读取初始化配置参数并输出</p></li> <li><p>ServletConfigServlet代码</p></li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ServletConfigServlet</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span> <span class="token punctuation">{</span>
    
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token comment">// 获得配置对象</span>
        <span class="token class-name">ServletConfig</span> config <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getServletConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 获得初始化参数值</span>
        <span class="token class-name">String</span> encoding <span class="token operator">=</span> config<span class="token punctuation">.</span><span class="token function">getInitParameter</span><span class="token punctuation">(</span><span class="token string">&quot;encoding&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">&quot;utf-8&quot;</span><span class="token punctuation">.</span><span class="token function">equalsIgnoreCase</span><span class="token punctuation">(</span>encoding<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment">// 模拟响应数据给浏览器</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;执行utf-8编码操作&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token comment">// 模拟响应数据给浏览器</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;执行gbk编码操作&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><ul><li>配置ServletConfigServlet初始化参数</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200423165815380.png" alt="image-20200423165815380"></p> <ul><li>部署项目并浏览器访问地址如下：</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200423170021077.png" alt="image-20200423170021077"></p> <h3 id="_3-7-loadonstartup-属性-了解"><a href="#_3-7-loadonstartup-属性-了解" class="header-anchor">#</a> 3.7 loadOnStartup 属性（了解）</h3> <h4 id="_3-7-1-思考问题"><a href="#_3-7-1-思考问题" class="header-anchor">#</a> 3.7.1 思考问题</h4> <p>​		问：如果一个servlet在创建对象或者是初始化的时候需要执行耗时操作：比如加载配置文件并封装信息到对象上，这样就必然导致第一个访问的用户要等待比较长的时间，用户体验非常差。如何解决这个问题呢？</p> <h4 id="_3-7-2-作用"><a href="#_3-7-2-作用" class="header-anchor">#</a> 3.7.2 作用</h4> <ul><li>让 web 容器启动的时候创建并初始化 Servlet。</li></ul> <h4 id="_3-7-3-用法"><a href="#_3-7-3-用法" class="header-anchor">#</a> 3.7.3 用法</h4> <ul><li>web.xml配置用法</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200423155754773.png" alt="image-20200423155754773"></p> <ul><li>注解配置用法</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200423155954287.png" alt="image-20200423155954287"></p> <p><strong>说明：取值范围1到10，值越小越先加载。默认值是-1：代表第1次访问时创建对象和初始化</strong></p> <h4 id="_3-7-4-使用场景"><a href="#_3-7-4-使用场景" class="header-anchor">#</a> 3.7.4 使用场景</h4> <p>​		在后续学习的框架中，有些核心的Servlet需要在服务器启动时创建并做初始化操作。比如 SpringMVC 的核心控制器，如下配置：</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200423160427877.png" alt="image-20200423160427877"></p> <h3 id="_3-8-servlet映射细节-了解"><a href="#_3-8-servlet映射细节-了解" class="header-anchor">#</a> 3.8 Servlet映射细节（了解）</h3> <h4 id="_3-8-1-配置多个路径"><a href="#_3-8-1-配置多个路径" class="header-anchor">#</a> 3.8.1 配置多个路径</h4> <h5 id="_3-8-1-1-配置方式1"><a href="#_3-8-1-1-配置方式1" class="header-anchor">#</a> 3.8.1.1 配置方式1</h5> <ul><li>一个 <code>&lt;servlet-mapping&gt;</code> 中写多个 <code>&lt;url-pattern&gt;</code></li></ul> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-mapping</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-name</span><span class="token punctuation">&gt;</span></span>HelloServlet<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-name</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>url-pattern</span><span class="token punctuation">&gt;</span></span>/demo1.html<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>url-pattern</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>url-pattern</span><span class="token punctuation">&gt;</span></span>/demo2.html<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>url-pattern</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-mapping</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h5 id="_3-8-1-2-配置方式2"><a href="#_3-8-1-2-配置方式2" class="header-anchor">#</a> 3.8.1.2 配置方式2</h5> <ul><li>一个<code>&lt;servlet&gt;</code>对应多个<code>&lt;servlet-mapping&gt;</code></li></ul> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-mapping</span><span class="token punctuation">&gt;</span></span>
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-name</span><span class="token punctuation">&gt;</span></span>HelloServlet<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-name</span><span class="token punctuation">&gt;</span></span>
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>url-pattern</span><span class="token punctuation">&gt;</span></span>/demo1.html<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>url-pattern</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-mapping</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-mapping</span><span class="token punctuation">&gt;</span></span>
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>servlet-name</span><span class="token punctuation">&gt;</span></span>HelloServlet<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-name</span><span class="token punctuation">&gt;</span></span>
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>url-pattern</span><span class="token punctuation">&gt;</span></span>/demo2.html<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>url-pattern</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>servlet-mapping</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>注意： <code>&lt;url-pattern&gt;</code> 中的内容必须唯一，必须以 / 开头。</p> <h4 id="_3-8-2-通配符映射"><a href="#_3-8-2-通配符映射" class="header-anchor">#</a> 3.8.2 通配符映射 *</h4> <table><thead><tr><th>通配符格式</th> <th>说明</th></tr></thead> <tbody><tr><td><code>/*</code><br><code>/目录名/*</code></td> <td>/* ：匹配所有的访问地址，必须以 / 开头<br>/admin/*：匹配 admin 目录下的所有地址</td></tr> <tr><td><code>*.扩展名</code></td> <td>匹配某个扩展名结尾的访问地址。如：*.action  *.do</td></tr></tbody></table> <blockquote><p>注意：不能同时 <code>/*.扩展名</code> 的访问路径：会导致整个web项目加载失败，所有的web资源都不能访问。项目启动会报如下错误：</p></blockquote> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code>Caused by: java.lang.IllegalArgumentException: Invalid <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>url-pattern</span><span class="token punctuation">&gt;</span></span> /admin/*.action in servlet mapping
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="_3-8-3-映射注意事项"><a href="#_3-8-3-映射注意事项" class="header-anchor">#</a> 3.8.3 映射注意事项</h4> <ul><li><p>映射路径必须保证唯一性，除通配符结合扩展名使用，否则必须以 / 开头</p></li> <li><p>servlet-name 不能叫做 default 和 jsp</p> <ul><li>Tomcat内部已有两个Servlet，一个叫default，一个叫 jsp。</li> <li>DefaultServlet 是用来处理静态资源</li> <li>JSPServlet 是用来将 JSP 文件翻译成 Java 文件</li> <li>如果我们的名字和内部的重名了，则我们的 servlet 会覆盖内部的 servlet。在 tomcat 的配置文件中 conf/web.xml, 该文件是当前服务器所有的项目都可以公用的信息.</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200423162702463.png" alt="image-20200423162702463"></p></li></ul> <h4 id="_3-8-4-面试题"><a href="#_3-8-4-面试题" class="header-anchor">#</a> 3.8.4 面试题</h4> <ul><li>创建 2 个 Servlet，一个 Servlet1，一个 Servlet2，在下列情况下，访问哪个Servlet</li></ul> <table><thead><tr><th>请求URL</th> <th>Servlet1</th> <th>Servlet2</th> <th>访问哪个</th></tr></thead> <tbody><tr><td>/abc/a.html</td> <td>/abc/*</td> <td>/*</td> <td>Servlet1</td></tr> <tr><td>/abc</td> <td>/abc/*</td> <td>/abc</td> <td>Servlet2</td></tr> <tr><td>/abc/a.do</td> <td>/abc/*</td> <td>*.do</td> <td>Servlet1</td></tr> <tr><td>/a.do</td> <td>/*</td> <td>*.do</td> <td>Servlet1</td></tr> <tr><td>/xxx/yyy/a.do</td> <td>/*</td> <td>*.do</td> <td>Servlet1</td></tr></tbody></table> <ul><li>结论
<ul><li>优先级：/开头的优先级大于扩展名结尾</li> <li>匹配原则：精确匹配的原则，哪个更匹配就使用那个</li></ul></li></ul> <h2 id="四、小结与作业"><a href="#四、小结与作业" class="header-anchor">#</a> 四、小结与作业</h2> <h3 id="_4-1-小结"><a href="#_4-1-小结" class="header-anchor">#</a> 4.1 小结</h3> <ul><li><p>理解 HTTP 协议 作用</p></li> <li><p>掌握使用浏览器来查看请求和响应信息</p></li> <li><p>掌握请求和响应信息的结构</p></li> <li><p>掌握 GET 和 POST请求的区别</p></li> <li><p>理解 Servlet 作用,可以帮咱们完成什么功能?</p></li> <li><p>掌握 第一个helloword 程序的开发(2.5 的 web.xml 配置方式,3.0  的注解使用方式)</p></li> <li><p>理解并掌握 Servlet 的生命周期</p> <ul><li><p>首次访问 Servlet 时 创建对象并调用 init 方法,然后调用 service 方法</p></li> <li><p>非首次访问,直接调用 service 方法</p></li></ul></li> <li><p>正常关闭服务器时调用销毁方法 destroy 方法</p></li> <li><p>掌握 Servlet 的请求流程 (能独立画流程图或用文字的形式写清请求流程) 浏览器 -- 服务器Servlet 的过程</p></li> <li><p>掌握使用 ServletConfig 对象来解决硬编码问题即可,(会配置web.xml ,能Servlet中获取web.xml 中的配置信息即可)</p></li> <li><p>理解 loadOnStartup 配置的作用即可(配置下,看下效果即可)</p></li></ul> <h3 id="_4-2-作业"><a href="#_4-2-作业" class="header-anchor">#</a> 4.2 作业</h3> <ul><li><p>书写两个表单，一个是 get 请求一个 post 请求，观看 get  和 post 请求的区别，通过浏览器来查看请求和响应信息（注册:账号，邮箱，手机号码）</p></li> <li><p>书写 Servlet 2.5 和 3.0 的 hello 程序（在2.5中完成数组中数据统计，int[]{1,3,4,5,6,9}）</p></li> <li><p>书写 Servlet 来演示 Servlet 的生命周期</p></li> <li><p>理解并自行书写Servlet 的请求流程（图文 || 文字）</p></li> <li><p>书写 Servlet 来演示 ServletConfig 对象的使用(获取账号和密码==&gt;【可拓展自动登录功能】)</p></li> <li><p>简单使用 loadOnStarup 属性</p></li></ul></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=Http" title="标签">#Http</a><a href="/tags/?tag=Servlet" title="标签">#Servlet</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/1125b7/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">网络编程与Tomcat</div></a> <a href="/pages/9902e4/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">请求与响应</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/1125b7/" class="prev">网络编程与Tomcat</a></span> <span class="next"><a href="/pages/9902e4/">请求与响应</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/39.2cb0b4ee.js" defer></script>
  </body>
</html>
